﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


namespace DAL
{
    public class Messages
    {
        public bool GetCorrespondentsList(string username, out List<Util.LastMessage> corList)
        {
            corList = new List<Util.LastMessage>();
            try
            {
                SqlDataReader sdr;

                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["FProj"].ConnectionString;
                    con.Open();
                    String strSQL = "[GetCorrespondentsList]";
                    SqlCommand comm = new SqlCommand(strSQL, con);
                    comm.CommandType = CommandType.StoredProcedure;

                    SqlParameter param = new SqlParameter();
                    param.ParameterName = "@UserName";
                    param.Value = username;
                    param.SqlDbType = SqlDbType.NVarChar;
                    comm.Parameters.Add(param);

                    sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);

                    if (sdr != null)
                    {
                        while (sdr.Read())
                        {
                            corList.Add(new Util.LastMessage { FromWhom = sdr["UserName"].ToString(), Date = "s" });
                        }
                    }

                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool GetMessageList(string towhom, string fromwhom, out List<Util.Message> messList)
        {
            messList = new List<Util.Message>();
        
                SqlDataReader sdr;

                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["FProj"].ConnectionString;
                    con.Open();
                    String strSQL = "[GetMessageList]";
                    SqlCommand comm = new SqlCommand(strSQL, con);
                    comm.CommandType = CommandType.StoredProcedure;

                    SqlParameter param = new SqlParameter();
                    param.ParameterName = "@fromwhomName";
                    param.Value = fromwhom;
                    param.SqlDbType = SqlDbType.NVarChar;
                    comm.Parameters.Add(param);

                    SqlParameter param2 = new SqlParameter();
                    param2.ParameterName = "@towhomName";
                    param2.Value = towhom;
                    param2.SqlDbType = SqlDbType.NVarChar;
                    comm.Parameters.Add(param2);

                    sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);

                    if (sdr != null)
                    {
                        while (sdr.Read())
                        {
                            messList.Add(new Util.Message { Text = sdr["Text"].ToString(), SendingDate = sdr["SendingDate"].ToString(), ToWhom = sdr["ToWhom"].ToString(), FromWhom = sdr["FromWhom"].ToString() });
                        }
                    }

                }
       
            return true;
        }

        public bool SendMessage(Util.Message mes)
        {
            int count;

            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = ConfigurationManager.ConnectionStrings["FProj"].ConnectionString;

                String strSQL = "INSERT INTO Messages (Text, SendingDate, ToWhom, FromWhom, ReceptionDate) VALUES (@Text, @SendingDate, @ToWhom, @FromWhom, '11.11.11')";
                SqlCommand comm = new SqlCommand(strSQL, con);

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@Text";
                param.Value = mes.Text;
                param.SqlDbType = SqlDbType.Text;
                comm.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@SendingDate";
                param.Value = mes.SendingDate;
                param.SqlDbType = SqlDbType.DateTime;
                comm.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@ToWhom";
                param.Value = mes.ToWhom;
                param.SqlDbType = SqlDbType.NVarChar;
                comm.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@FromWhom";
                param.Value = mes.FromWhom;
                param.SqlDbType = SqlDbType.NVarChar;
                comm.Parameters.Add(param);

                con.Open();

                count = comm.ExecuteNonQuery();
            }
            if (count > 0)
                return true;
            else
                return false;
        }
    }
}